package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/demo")
    @ResponseBody
    public String demo(){
        return "框架整合初步完成";
    }

    @RequestMapping("/userList")
    public String userList(Model model){
        List<User> list = userService.findAll();
        model.addAttribute("userList", list);
        return "userList";
    }

    @RequestMapping("/toAxiosUserList")
    public String toAxiosUserList(){
        return "AxiosUserList";
    }

    /**
     * 新增，restFul风格，重定向到userList
     * /user/tom/18/男
     *
     * 可以使用对象接收
     */
    @GetMapping("/user/{name}/{age}/{sex}")
    public String putUser(User user){
        userService.insertUser(user);
        return "redirect:/userList";
    }
}
